package com.chenliang.controller;

import com.chenliang.domain.Permission;
import com.chenliang.domain.Role;
import com.chenliang.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @ProjectName: mavenSSM
 * @Package: com.chenliang.controller
 * 作用：
 */
@Controller
@RequestMapping("/Role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/findAll")
    public String findAll(Model model,
                          @RequestParam(name = "page", defaultValue = "1", required = true) Integer page,
                          @RequestParam(name = "pageSize", defaultValue = "3", required = true) Integer pageSize) {

        List<Role> roles = roleService.findAll(page, pageSize);
        PageInfo pageInfo = new PageInfo(roles);
        model.addAttribute("pageInfo", pageInfo);
        return "role-list";
    }

    @RequestMapping("/addRole")
    public String addRole(Role role) throws Exception {
        roleService.addRole(role);

        return "redirect:/Role/findAll";

    }

    //角色详情
    @RequestMapping("/findById")
    public ModelAndView findById(String id) {
        Role role = roleService.findById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("RoleById", role);
        mv.setViewName("role-show");
        return mv;
    }


    //    查询该角色还没有的权限
    @RequestMapping("/addPermission")
    public String addPermission(Model model, String id) {
        List<Permission> permissions = roleService.findRoleByPermission(id);
        model.addAttribute("permission", permissions);
        model.addAttribute("roleId", id);
        return "role-permission-add";
    }

    //    添加该角色的新权限
    @RequestMapping("/addPermissionMeth")
    public String addPermissionMeth(Model model, @RequestParam(name = "roleId") String roleId, @RequestParam(name = "ids") String[] ids) {
        roleService.addPermission(roleId, ids);

        Role role = roleService.findById(roleId);
        model.addAttribute("RoleById",role);
        return "role-show";

    }
}
